package com.example.demo.controller;

import com.example.demo.entity.Comment;
import com.example.demo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 评论控制器
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 根据新闻ID获取评论
    @GetMapping("/news/{newsId}")
    public List<Comment> getCommentsByNewsId(@PathVariable Long newsId) {
        return commentService.getCommentsByNewsId(newsId);
    }

    // 插入新的评论
    @PostMapping("/news/{newsId}")
    public void insertComment(@PathVariable Long newsId, @RequestBody Comment comment) {
        comment.setNewsId(newsId);
        commentService.insertComment(comment);
    }

    // 根据ID删除评论
    @DeleteMapping("/{id}")
    public void deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
    }
}
